﻿Public Class SearchNew
    Inherits CoreClass

    Dim strSearch As String = ""
    Dim pageNum As Integer = 1
    Dim numPerPage As Integer = 10

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            ' Lấy biến Số lượng sản phẩm show ra mỗi trang ở biến Config, có thể thay đổi được ở Web.config
            numPerPage = Integer.Parse(objConfig.ProductPerPage)
        Catch ex As Exception

        End Try
        Try
            ' Request biến Trang hiện tại từ URL, nếu k có thì mặc định là trang 1
            pageNum = Integer.Parse(Request.QueryString("p"))
        Catch ex As Exception

        End Try

        Try
            strSearch = Request.QueryString("s")
            'Response.Write("string = " & strSearch & "<BR>page = " & page)
            txtSearch.Text = strSearch
            txtSearch.Attributes.Add("onkeydown", "javascript:KeyCodePage(event);")
            txtSearch.Attributes.Add("onfocus", "SearchOnFocus(this)")
            txtSearch.Attributes.Add("onblur", "SearchOnBlur(this)")
        Catch ex As Exception

        End Try

        Try
            sql = "spo_ProductNewGetListSearch "
            sql &= objFunction.ValueCheck(1) & ", "  ' 0 = Product, 1 = New
            sql &= objFunction.ValueCheck(pageNum) & ", "
            sql &= objFunction.ValueCheck(numPerPage) & ", "
            sql &= objFunction.FieldUniCk(strSearch)
            ' SQL trên return lại 3 result set theo thứ tự: 0-Danh sách sản phẩm, 1-Tổng số sản phẩm, 2-Tên category
            Dim dts As DataSet = objDataRead.ExeDataset(sql)
            ' Đưa dữ liệu của bảng 0 vào Repeater
            RepeatItems.DataSource = dts.Tables(0)
            RepeatItems.DataBind()
            RepeatItems.Dispose()
            ' Lấy ra tổng số sản phẩm trả về
            Dim totalPro As Integer = 0
            Dim row As DataRow
            row = dts.Tables(1).Rows(0)
            totalPro = Integer.Parse(row("Total").ToString)
            ' Show list điều hướng trang
            divNavigator.InnerHtml = getPageNavigator(pageNum, numPerPage, totalPro)
        Catch ex As Exception
            If objConfig.Developer.Equals("1") Then
                objAlert.ErrorBox(Page, "File: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.DeclaringType.Name & _
                                  " --- Function: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.Name & _
                                  " --- Mess: " & ex.Message.Replace("'", """"))
            Else
                objAlert.ErrorBox(Page, "Lỗi kỹ thuật, hãy thông báo lại với Admin, xin cảm ơn.")
            End If
        End Try
    End Sub

#Region "My Function"
    ' Build link đến chi tiết sản phẩm dựa trên tên danh mục, tên sp, id sp
    Public Function buildLinkDetailPro(ByVal cateName As String, ByVal proName As String, ByVal proId As String) As String
        'Return "/tin-tuc/" & cateName & "/" & proName & "-" & proId
        Return "/NewDetail.aspx?idNew=" & proId
    End Function

    ' Build src image
    Public Function buildImage(ByVal strDate As String, ByVal strImage As String) As String
        Return objFunction.buildImageByDate(strDate, strImage)
    End Function

    ' format phan Gioi thieu tin, giới hạn 200 ký tự
    Public Function getDesc(ByVal strTmp As String) As String
        Return objFunction.CutStringByInterger(strTmp, 200)
    End Function

    'format time cho tin bai
    Public Function buildTime(ByVal strTime As String, ByVal temp As String) As String
        Return objFunction.formatDateTimeToTemplate(strTime, temp)
    End Function

    'format date cho tin bai
    Public Function buildDate(ByVal strTime As String, ByVal temp As String) As String
        Return objFunction.formatDateTimeToTemplate(strTime, temp)
    End Function

    Public Function getPageNavigator(ByVal currPage As Integer, ByVal numPerPage As Integer, ByVal totalItem As Integer) As String
        Dim objPage As New DataPage
        Dim pager_url As String = ""
        Dim path As String = ""
        Dim inactive_page_tag As String = ""
        Dim previous_page_text As String = ""
        Dim next_page_text As String = ""
        Dim first_page_text As String = ""
        Dim last_page_text As String = ""

        Dim total_records As Integer = 0
        Dim start As Integer = 0
        Dim scroll_page As Integer = 0
        Dim per_page As Integer = 0
        Dim total_pages As Integer = 0
        Dim current_page As Integer = 0
        Dim page_links As String = ""
       
        ' CONFIG
        path = "/SearchNew.aspx?s=" & Request.QueryString("s") & "&p="
        total_records = totalItem ' lay tong so record
        scroll_page = 5 ' So trang dc hien thi o phan dieu huong
        per_page = numPerPage ' So ban ghi moi trang
        current_page = currPage
        pager_url = path ' redirect lai chinh tran hien tai voi tham so page
        inactive_page_tag = "id=""current_page"""
        previous_page_text = "Trang trước" ' Dinh dang cua link chuyen ve trang truoc
        next_page_text = "Trang sau" ' Dinh dang cua link chuyen den trang sau
        first_page_text = "Trang đầu" ' Dinh dang cua link chuyen ve trang dau tien
        last_page_text = "Trang cuối" ' Dinh dang cua link chuyen den trang cuoi cung

        objPage.pager_set(pager_url, total_records, scroll_page, per_page, current_page, inactive_page_tag, previous_page_text, next_page_text, first_page_text, last_page_text, "")

        Dim txtPageNavigator As String = ""

        If total_records > per_page Then

            txtPageNavigator &= "<ul class=""boxProNavigator"">"
            If Not objPage.first_page = "" Then
                txtPageNavigator &= objPage.first_page
            End If
            If Not objPage.previous_page = "" Then
                txtPageNavigator &= objPage.previous_page
            End If
            If Not objPage.page_links = "" Then
                If total_records >= per_page Then
                    txtPageNavigator &= objPage.page_links
                End If
            End If
            If Not objPage.next_page = "" Then
                txtPageNavigator &= objPage.next_page
            End If
            If Not objPage.last_page = "" Then
                txtPageNavigator &= objPage.last_page
            End If
            txtPageNavigator &= "</ul>"
        End If
        Return txtPageNavigator
    End Function
#End Region

End Class